package demo3;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class ListExample {
    public static void main(String[] args) {
        // 1. 创建 ArrayList 并添加元素
        List<String> list = new ArrayList<>();
        list.add("apple");
        list.add("banana");
        list.add("cherry");
        list.add("date");
        list.add("elderberry");

        // 2. 遍历 ArrayList
        System.out.println("Original list:");
        for (String element : list) {
            System.out.println(element);
        }

        // 3. 使用 Collections 工具类对 ArrayList 进行排序
        Collections.sort(list);
        System.out.println("\nSorted list:");
        for (String element : list) {
            System.out.println(element);
        }

        // 4. 删除指定元素
        list.remove("cherry");
        System.out.println("\nList after removing 'cherry':");
        for (String element : list) {
            System.out.println(element);
        }
    }
}
